package ac.java.utils;

import java.net.URLDecoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Pager
{

	protected Integer pageSize = 10;// 30; //每页显示多少条
	protected Integer showPage = 10; // 所显示出的页码数列表
	protected Integer rsTotal = 0; // 记录总数
	protected Integer curPg = 1; // 当前页

	//
	protected Integer total = 0; // 总页数
	protected Integer next; // 下一页
	protected Integer pre = 1; // 上一页
	protected Integer from;
	protected Integer to;
	protected Integer first = 1; // 首页
	protected Integer end = 1; // 末页
	protected Integer rowBegin = 0; // 开始行数
	protected Integer preRowBegin = 0;
	protected Integer nextRowBegin = 0;
	protected Integer showFirst = 1;
	protected Integer showEnd = 10;

	public Integer getPreRowBegin()
	{
		return preRowBegin;
	}

	public Integer getNextRowBegin()
	{
		return nextRowBegin;
	}

	//

	public void setShowPage(Integer showPage)
	{
		this.showPage = showPage;
	}

	public void setRsTotal(Integer rsTotal)
	{
		this.rsTotal = rsTotal;

	}

	public void setCurPg(Integer curPg)
	{
		if (curPg > 0)
		{
			this.curPg = curPg;
		}
		else
		{
			this.curPg = 1;
		}
		this.rowBegin = (this.curPg - 1) * this.pageSize;
	}

	public void getPager()
	{

		this.total = (int) Math.ceil(Double.valueOf(this.rsTotal) / Double.valueOf(this.pageSize));

		this.curPg = (this.rowBegin / this.pageSize) + 1;
		if (this.curPg < this.total)
		{
			this.next = this.curPg + 1;
		}
		else
		{
			this.next = this.total + 1;
			;
		}

		if (this.curPg > 0)
		{
			this.pre = this.curPg - 1;
		}

		Integer pgSt = curPg - (this.showPage / 2);
		pgSt = pgSt > 0 ? pgSt : 1;

		this.to = (pgSt + this.showPage) > this.total ? this.total : pgSt + this.showPage;
		this.from = pgSt;
		this.first = 1;
		this.end = total;
		setPreRowBegin(this.rowBegin);
		setNextRowBegin(this.rowBegin);

		this.showFirst = this.curPg - this.showPage / 2;
		if (this.showFirst <= 0)
		{
			this.showFirst = 1;
		}
		this.showEnd = this.curPg + this.showPage / 2;
		if (this.showEnd >= this.total)
		{
			this.showEnd = this.total;
		}
		else if (this.showEnd < 10 && this.total > 10)
		{
			this.showEnd = 10;
		}
	}

	public void setPager(HttpServletRequest request, HttpServletResponse response)
	{
		Map requestMap = request.getParameterMap();
		String acPgs = getPgsByCookie(request);

		if (requestMap.containsKey("pgs"))
		{
			Integer pgs = Integer.valueOf(request.getParameter("pgs"));
			if (pgs > 0)
			{
				this.pageSize = pgs;
				setPgsToCookie(response, pgs);
			}
		}
		else
		{
			if (!Vaild.isEmpty(acPgs))
			{
				this.pageSize = Integer.valueOf(acPgs);
			}
		}

		if (requestMap.containsKey("pg"))
		{
			this.curPg = Integer.valueOf(request.getParameter("pg"));
			this.rowBegin = (this.curPg - 1) * this.pageSize;
		}
		else if (requestMap.containsKey("rowBegin"))
		{
			this.rowBegin = Integer.valueOf(request.getParameter("rowBegin"));
		}

	}

	public void setPgsToCookie(HttpServletResponse response, Integer pgs)
	{
		Cookie cookie0 = new Cookie("acPgs", pgs.toString());
		response.addCookie(cookie0);
	}

	public String getPgsByCookie(HttpServletRequest request)
	{
		String acPgs = "";
		try
		{
			Cookie[] cookieArray = request.getCookies();
			if (Vaild.isEmpty(cookieArray))
			{
				return null;
			}
			for (Cookie cookie : cookieArray)
			{
				String cookieName = cookie.getName();
				if (cookieName.equals("acPgs"))
				{
					acPgs = URLDecoder.decode(cookie.getValue(), "UTF-8");
				}

			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return acPgs;

	}

	public Integer getShowFirst()
	{
		return showFirst;
	}

	public Integer getShowEnd()
	{
		return showEnd;
	}

	public String getPageUrl(HttpServletRequest request)
	{

		StringBuffer urlSb = request.getRequestURL();
		// System.out.println("--------------urlSb--"+urlSb);
		Map requestMap = request.getParameterMap();
		Iterator it = requestMap.entrySet().iterator();
		urlSb.append("?a=a");
		while(it.hasNext())
		{
			Entry entry = (Entry) it.next();

			String key = (String) entry.getKey();
			// pgs:相当于pageSize(每页显示多少条)；pg:相当于curPg(第几页)
			if (key.equals("pgs") || key.equals("pg") || key.equals("Submit") || key.equals("rd1")
					|| key.equals("rd2") || key.equals("rowBegin") || key.equals("a"))
			{
				continue;
			}
			String value = request.getParameter(key);

			urlSb.append("&" + key + "=" + value);

		}

		return urlSb.toString();
	}

	public Integer getTotal()
	{
		return total;
	}

	public void setTotal(Integer total)
	{
		this.total = total;
	}

	public Integer getNext()
	{
		return next;
	}

	public void setNext(Integer next)
	{
		this.next = next;
	}

	public Integer getPre()
	{
		return pre;
	}

	public void setPre(Integer pre)
	{
		this.pre = pre;
	}

	public Integer getFrom()
	{
		return from;
	}

	public void setFrom(Integer from)
	{
		this.from = from;
	}

	public Integer getTo()
	{
		return to;
	}

	public void setTo(Integer to)
	{
		this.to = to;
	}

	public Integer getFirst()
	{
		return first;
	}

	public void setFirst(Integer first)
	{
		this.first = first;
	}

	public Integer getEnd()
	{
		return end;
	}

	public void setEnd(Integer end)
	{
		this.end = end;
	}

	public Integer getShowPage()
	{
		return showPage;
	}

	public Integer getRsTotal()
	{
		return rsTotal;
	}

	public void setPageSize(Integer pageSize)
	{
		this.pageSize = pageSize;
	}

	public Integer getPageSize()
	{
		return pageSize;
	}

	public Integer getCurPg()
	{
		return curPg;
	}

	public Integer getRowBegin()
	{
		return rowBegin;
	}

	public void setRowBegin(Integer rowBegin)
	{
		this.rowBegin = rowBegin;
	}

	protected void setPreRowBegin(Integer rowBegin)
	{
		this.preRowBegin = (rowBegin - this.pageSize > 0) ? (rowBegin - this.pageSize) : 0;
	}

	protected void setNextRowBegin(Integer rowBegin)
	{
		this.nextRowBegin = (rowBegin + this.pageSize <= this.rsTotal) ? (rowBegin + this.pageSize)
				: 0;
	}
}
